package cn.tedu.boyou.acticle.service.impl;

import cn.tedu.boyou.acticle.domain.StrategyTag;
import cn.tedu.boyou.acticle.mapper.StrategyTagMapper;
import cn.tedu.boyou.acticle.service.IStrategyTagService;
import cn.tedu.boyou.common.query.StrategyTagQuery;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class StrategyTagServiceImpl implements IStrategyTagService {
    @Autowired
    private StrategyTagMapper strategyTagMapper;

    public PageInfo query(StrategyTagQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyTag> regions = strategyTagMapper.selectForList(qo);
        return new PageInfo(regions);
    }

    public void saveOrUpdate(StrategyTag strategyTag) {
        if(strategyTag.getId() != null){
            strategyTagMapper.updateByPrimaryKey(strategyTag);
        }else{
            strategyTagMapper.insert(strategyTag);
        }

    }

    public void delete(Long id) {
        strategyTagMapper.deleteByPrimaryKey(id);
    }

    public void save(String[] tags) {
        StrategyTag sTag = new StrategyTag();
        for (String tag : tags) {
            sTag.setName(tag);
            strategyTagMapper.insert(sTag);
        }
    }

    public String selectTagsByDetailId(Long id) {
        return strategyTagMapper.selectTagsByDetailId(id);
    }

    public List<StrategyTag> listAll() {
        return strategyTagMapper.selectAll();
    }

    public List<String> getByDestId(Long id) {

        return strategyTagMapper.selectByDestId(id);
    }

    public List<StrategyTag> getByTagId(List<String> list) {
        ArrayList<StrategyTag> tags = new ArrayList<>();
        for (String s : list) {
            StrategyTag tag = strategyTagMapper.selectByPrimaryKey(Long.valueOf(s));
            tags.add(tag);
        }
        return tags;
    }
}
